layui.use(['element','table','laytpl'],function () {
    var $ = layui.$,
        table = layui.table,
        layer = layui.layer;
    var tableIns = table.render({
        elem: '#demo'
        ,id:"mytable"
        ,url: '/receipt/list' //数据接口
        ,page: true //开启分页
        ,limit:15
        ,limits:[15,30,60,100]
        ,response: {
            statusName: 'code' //数据状态的字段名称，默认：code
            ,statusCode: 200 //成功的状态码，默认：0
            ,msgName: 'msg' //状态信息的字段名称，默认：msg
            ,countName: 'total' //数据总数的字段名称，默认：count
            ,dataName: 'rows' //数据列表的字段名称，默认：data
        }
        ,cols: [[ //表头
            {type:'checkbox'}
            ,{title: '序号',type:'numbers'}
            // ,{field: 'code', title: '收费单编号',width:'15%'}
            ,{field: 'elderlyName', title: '老人姓名'}
            ,{field: 'name', title: '收费项目'}
            ,{field: 'amount', title: '收款金额',templet:function(d){
                    return "￥ " + d.amount;
                }}
            ,{ title: '收费单状态',templet:function(d){
                switch (d.status){
                    case 1: return "待支付";break;
                    case 3: return "已支付";break;
                    case 4: return "逾期";break;
                    case 5: return "作废";break;
                }
             }}
            ,{title: '收费单类型',templet:function(d){
                    switch (d.type){
                        case 1: return "服务收费单";break;
                        case 2: return "套餐收费单";break;
                        case 3: return "其它收费单";break;
                        default:return "";
                    }
            }}
            ,{title: '创建时间',templet:function(d){
                    return $.dateFormat(new Date(d.createdTime),"yyyy-MM-dd");
                }}
          /*  ,{title: '缴费时间',templet:function(d){
                    if(d.status != 3) return "";
                    return $.dateFormat(new Date(d.paymentTime),"yyyy-MM-dd");
                }}*/
            ,{title:'操作',toolbar:"#toolbar",width:"35%"}

        ]]
    });

    //工具条
    table.on("tool(test)",function(obj){
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        if("edit" == layEvent){
            location.href = "/receipt/edit?id=" + data.id;
        }else if("delete" == layEvent){
            location.href = "/receipt/delete?id=" + data.id;
        }else if("cashPay" == layEvent){
            location.href = "/receipt/payment?id=" + data.id+"&type=3&elderlyName=" + data.elderlyName;
        }else if("detail" == layEvent){
            location.href = "/receipt/detail?id=" + data.id;
        }else if("onlinePay" == layEvent){  //在线支付
            layer.open({
                title: '温馨提示'
                ,content: '尚未开通在线支付，无法使用此功能。联系电话 13572299477'
            });
            // location.href = "/receipt/payment?id=" + data.id+"&type=1";
        }else if("sedReceipt" == layEvent){  //远程收费
            // $("#qrcode").empty();
            // $("#qrcodeWrap > #info").empty();
            // $("#qrcodeWrap > #info").text(data.name +"-" +data.elderlyName+"-"+ data.orgName);
            // var qrcode = new QRCode('qrcode', {
            //     text: 'https://www.kerry365.com/receipt/receipt_pay?receiptId=' + data.id,
            //     width: 256,
            //     height: 256,
            //     colorDark : '#000000',
            //     colorLight : '#ffffff',
            //     correctLevel : QRCode.CorrectLevel.H
            // });
            // var index = layer.open({
            //     title:'发送账单(截图发送至微信)',
            //     type: 1,
            //     content: $('#qrcodeWrap')
            // });
            layer.open({
                title: '温馨提示'
                ,content: '尚未开通远程支付，无法使用此功能。联系电话 13572299477'
            });
        }

    });

    //批量删除
    $("#deleteAll").on('click',function(){
        var checkStatus = layui.table.checkStatus('mytable'); //test即为基础参数id对应的值
        if(checkStatus.data.length < 1) return false;
        var params = '';
        $.each(checkStatus.data,function(i,v){
            params += "id=" + v.id + "&";
        })
        //删除
        location.href = "/receipt/delete?" + params;

    });

    /**
     * 搜索
     */
    $("#search").on('click',function(){
        var params = $("#search-form").serializeObject();
        tableIns.reload({
            where: params //设定异步数据接口的额外参数
        });
    });
    $("#reset").on('click',function(){
        tableIns.reload({
            where: {} //设定异步数据接口的额外参数
        });
    });


});